<?php
defined( "_SBKH_" ) or die( 'ERROR : RESTRICT ACCESS THIS LOCATION.' );	
require_once( ROOT . 'config/SBKH.class.php' );
class Route extends SBKH{
	public function __construct($tmp = ""){
			$sbkh = new SBKH();			
			if($tmp != ""){$sbkh->SetLayout($tmp);}
			//IF you want use Sample Controller
			$this -> SwitchPage($sbkh->getController());
			//By Defualt 
			//$sbkh -> Render();
	}
	public static function SwitchPage($controller = ""){
		//$delimiter = array('-','_');
		$delimiter = "-";
		$words = explode($delimiter,$controller);
		$className = "";
		foreach($words as $word){
			$className .= 	ucwords($word);
		}
		$obj = new SBKH();
		
		$filename = CONTROLLER . $className . 'Controller.php';	
		
		if(file_exists($filename)){
			$model = MODEL . $className . "Model.php";
			if(file_exists($model)){
				require_once $model;
			}
			require_once $filename;
			$action = getURL('action');
			$action = $action != ""?str_replace('-','_',$action):'index';			
			
			if(class_exists($className)){
				$con = new $className();
				if(method_exists($con,$action)){
					call_user_func_array(array($con,$action),getParams());										
				}else{					
					$obj -> SetURLTitle('SBKH:: Sorry method not found!!!');
					$obj -> SetLayout('error');
					$_skh_view = VIEW . 'errors/error_method.php';
					$content = file_get_contents($_skh_view);						
					$obj -> SetContent($content);
					$obj -> Render();
				}
			}else{
				$obj -> SetURLTitle('SBKH:: Sorry class not found!!!');
				$obj -> SetLayout('error');								
				$obj -> SetContent('<h1>ERROR: Class [' . $className . '] Not Found!!!</h1>');
				$obj -> Render();
			}
		}else{			
			$obj -> SetURLTitle('SBKH:: Sorry controller not found!!!');
			$obj -> SetLayout('error');								
			$obj -> SetContent('<h1>ERROR: Controller [' . $className . 'Controller.php] Not Found!!!</h1>');
			$obj -> Render();
		}
	}
}
?>